import api from '@/api/index'
const doEnum = obj => {
    let enums = {}
    Object.keys(obj).forEach(ks => {
        let kst = ''
        kst = (ks.indexOf('org.dutall.enums.') != -1 ) ? (ks.split('org.dutall.enums.'))[1] : ks
        if (!enums[kst]) enums[kst] = []
        Object.keys(obj[ks]).forEach(kks => {
            enums[kst].push(
                {
                    label: obj[ks][kks],
                    value: kks
                }
            )
        })
    })
    return enums
}
export default {
    namespaced: true,
    state: {
        enumAll: localStorage.getItem('enumAll') && JSON.parse(localStorage.getItem('enumAll')) || {}, // 通用枚举类
    },
    getters: {
        getEnumAll (state) {
            return state.enumAll
        }
    },
    mutations: {
        saveEnumAll (state, prov) {
          localStorage.setItem('enumAll', JSON.stringify(prov))
          state.enumAll = prov
        },
    },
    actions: {
        saveEnumAllAction ({commit}) {
            api.common.getCommonAllEunm().then(r => {
                let obj = r.data || {}
                obj = doEnum(obj)
                commit('saveEnumAll', obj)
            })
        }
    }
}